package cn.lingyangwl.agile.pay.pay.channel;

import cn.lingyangwl.framework.tool.core.exception.Assert;
import cn.lingyangwl.agile.pay.constant.IfCodeEnum;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author shenguangyang
 */
@Slf4j
public abstract class AbstractChannelUserService implements IChannelUserService {
    /**
     * 存放支付接口代码
     */
    protected static final Map<IfCodeEnum, IChannelUserService> serviceMap = new ConcurrentHashMap<>();

    @PostConstruct
    public void abstractChannelUserServiceInit() {
        IfCodeEnum ifCode = this.getIfCode();
        Assert.notNull(ifCode, "ifCode is null, class: " + this.getClass().getName());
        serviceMap.put(ifCode, this);
        log.info("load channelUserService, class: {}", this.getClass().getSimpleName());
    }

    /**
     * 获取服务
     */
    public static IChannelUserService getService(IfCodeEnum ifCodeEnum) {
        Assert.notNull(ifCodeEnum, "ifCode is null");
        return serviceMap.get(ifCodeEnum);
    }

    public static IChannelUserService getService(String ifCode) {
        return getService(IfCodeEnum.ofByCode(ifCode));
    }
}
